﻿using MediatR;
using S.PurchaseAndSales.Domain.Model;
using S.PurchaseAndSales.Infrastructure.Interface.PurchaseForm;
using S.PurchaseAndSales.Read.Api.Application.Command.PurchaseFormCommand;
using S.PurchaseAndSales.Read.Api.Dto;

namespace S.PurchaseAndSales.Read.Api.Application.CommandHandlers.PurchaseFormHandler
{
    /// <summary>
    /// 获取供应商下拉框
    /// </summary>
    public class VendorGetSelectCommandHandler : IRequestHandler<VendorGetSelectCommand, List<GetSelectVenDorDto>>
    {
        private readonly IVendorRepository repository;

        public VendorGetSelectCommandHandler(IVendorRepository repository)
        {
            this.repository = repository;
        }
        public async Task<List<GetSelectVenDorDto>> Handle(VendorGetSelectCommand request, CancellationToken cancellationToken)
        {
            List<VendorModel> list = await repository.GetAllAsync();
            List<GetSelectVenDorDto> venDorList = new List<GetSelectVenDorDto>();
            list.ForEach(v =>
            {
                GetSelectVenDorDto dto = new GetSelectVenDorDto()
                {
                    Value = v.VendorSnowId,
                    Label = v.VendorName
                };
                venDorList.Add(dto);
            });
            return venDorList;
        }
    }
}
